package it.polimi.swKnights.SWIMv2.general;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class UserDisplayTagHandler extends TagSupport
{

	private static final long serialVersionUID = 3204183610940205531L;
	private String username;
	private String name;
	private String surname;
	private String email;
	private String category;
	private String image;
	private String city;
	private String ability;

	@Override
	public int doStartTag() throws JspException
	{
		try
		{
			// Get the writer object for output.
			JspWriter out = pageContext.getOut();
			String inTheHtml = "";

			this.nullToEmptyFields();
			
			inTheHtml = this.createIdCardFrom(username, surname, name, email,
					image, city, ability);

			out.println(inTheHtml);

		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

		return SKIP_BODY;
	}

	private String createIdCardFrom(String username, String surname,
			String name, String email, String image, String city, String ability)
	{

		String res = "";

		String image_dec = decorate_image(image);

		String user_dec = decorate(username, "username", category);
		
		String ability_dec = decorate(ability, "ability", category);
		
		String surname_dec = decorate(surname, "surname", category);

		String name_dec = decorate(name, "name", category);

		String mail_dec = decorate(email, "email", category);
		
		String city_dec = decorate(city, "city", category);
		

		res = "" + image_dec + "<div>" + user_dec + ability_dec + surname_dec + name_dec + mail_dec + city_dec + "</div>";

		return res;

	}

	private String decorate_image(String image)
	{

		if (image == null || image.length() == 0)
		{
			return "";
		}
		String imgString = "";
		String user = "";
		if (this.username.equals("") || this.username == null)
		{
			if (this.name != null && (!this.name.equals("")))
			{
				user = this.name;
			}
			if (this.surname != null && (!this.surname.equals("")))
			{
				user = this.surname;
			}
		}
		else
		{
			user = this.username;
		}

		imgString = imgString.concat("<img src=" + "\"");
		imgString = imgString.concat(image);
		imgString = imgString.concat("\"");
		imgString = imgString.concat(" alt=" + "\"" + user + "_picture" + "\"");
		imgString = imgString.concat(" width=150 height=150 align='right' "
				+ "/>");

		return imgString;

	}

	private String decorate(String param, String paramName, String category)
	{
		// es cuoco ability category
		// es edo name name_and_surname
		// es "" ability name_and_surname
		// es "" surname username

		if (param.equals(""))
		{
			return "";
		}

		/*
		 * CATEGORY: username, name_surname, ability, name, surname, advanced
		 */
		

		
		if (category.equals("ability"))
		{
			
			if (paramName.equals("ability"))
			{
				return decorateAsImportant(param, paramName);
			}
			else
			{
				return decorateAsNormal(param, paramName);
			}
		}
		else if(category.equals("advanced"))
		{
			if(ability.length() > 0)
				if(paramName.equals("ability"))
				{
					return decorateAsNormal(param, paramName);
				}
			
		}
		else if (category.equals("friend"))
		{
			if (paramName.equals("email"))
			{
				return "";
			}
		}
		else if (category.equals("suggested"))
		{
			if (paramName.equals("name") || paramName.equals("surname"))
			{
				return "";
			}
		}
		else if (category.equals("ability") == false)
		{
			
			if (paramName.equals("ability"))
			{
				return "";
			}
		}

		// from here category is not ability

		if (category.equals("name") && paramName.equals("name"))
		{
			return this.decorateAsImportant(param, paramName);
		}
		else if (category.equals("surname") && paramName.equals("surname"))
		{
			return this.decorateAsImportant(param, paramName);
		}
		else if (category.equals("name_surname")
				&& (paramName.equals("name") || paramName.equals("surname")))
		{
			return this.decorateAsImportant(param, paramName);
		}
		else if (category.equals("username") && paramName.equals("username"))
		{
			return this.decorateAsImportant(param, paramName);
		}
		else if (category.equals("advanced"))
		{
			return this.decorateAsNormal(param, paramName);
		}
		else
		{
			return decorateAsNormal(param, paramName);
		}

	}

	private String decorateAsNormal(String param, String paramName)
	{
		param = addProfileLinkIfUsername(param, paramName);
		return "" + paramName + " : " + param + "" + "<br />";
	}

	private String decorateAsImportant(String param, String paramName)
	{
		param = addProfileLinkIfUsername(param, paramName);
		return "<b>" + paramName + " : " + param + "</b>" + "<br />";

	}

	private String addProfileLinkIfUsername(String param, String paramName)
	{
		if (paramName.equals("username"))
		{
			return "<a href=" + "\"" + "./Profile?username=" + param + "\""
					+ ">" + param + "</a>";
		}

		return param;

	}

	private void nullToEmptyFields()
	{
		if (this.name == null)
			name = "";
		if (this.category == null)
			category = "";
	
		if (this.surname == null)
			surname = "";
	
		if (this.username == null)
			username = "";
	
		if (this.email == null)
			email = "";
		
		if(this.ability == null)
			ability = "";
	
		if (image != null && image.length() == 0)
		{
	
			this.image = "http://placekitten.com/150/150";
	
		}
		else if (this.image == null)
		{
			this.image = "";
	
		}
		else
		{
			this.image = WebsiteParameters.createImagePath(this.image);
		}
	
		
		if(this.city == null)
		{
			this.city = "";
		}
			
		
	}

	public String getUsername()
	{
		return this.username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getSurname()
	{
		return surname;
	}

	public void setSurname(String surname)
	{
		this.surname = surname;
	}

	public String getEmail()
	{
		return email;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	public String getCategory()
	{
		return category;
	}

	public void setCategory(String category)
	{
		this.category = category;
	}

	public String getImage()
	{
		return image;
	}

	public void setImage(String image)
	{
		this.image = image;
	}


	public String getCity() {
		return city;
	}


	public void setCity(String city) {
		this.city = city;
	}

	public String getAbility()
	{
		return ability;
	}

	public void setAbility(String ability)
	{
		this.ability = ability;
	}

}